package com.harvey.coding.lambda.stream;

import cn.hutool.core.map.MapUtil;
import com.harvey.coding.lambda.cart.CartService;
import com.harvey.coding.lambda.cart.Sku;

import java.util.*;
import java.util.stream.Collectors;

public class StreamService {
    /**
     * 使用stream来模拟分页操作
     * @param cartSkuList 列表
     * @param limit 每页数量
     * @param offset 偏移量
     * @return 分页后的数组
     */
    public List<Sku> useStreamStimulatePagination(List<Sku> cartSkuList, int limit, int offset) {
        return cartSkuList.stream().sorted(Comparator.comparing(Sku::getSkuPrice).reversed()).skip(offset - 1).limit(limit).collect(Collectors.toList());
    }

    /**
     * 当无状态操作后面跟的是终端操作
     * @param cartSkuList
     */
    public void statelessThenTerminal(List<Sku> cartSkuList){
        cartSkuList.stream().peek( sku -> System.out.println(sku.getSkuName())).forEach(sku -> System.out.println(sku.toString()));
    }

    public void userFlatmapSplitWords() {
        List<Map<String, Map<String, String>>> l = new ArrayList<>();
        Map<String, Map<String, String>> doubleMapA = new TreeMap<>();
        Map<String, String> innerMap1 = new TreeMap<String, String>(){{
            put("Ai1-1", "Aiv1-1");
            put("Ai1-2", "Aiv1-2");
            put("Ai1-3", "Aiv1-3");
        }};
        Map<String, String> innerMap2 = new TreeMap<String, String>(){{
            put("Ai2-1", "Aiv2-1");
            put("Ai2-2", "Aiv2-2");
            put("Ai2-3", "Aiv2-3");
        }};
        Map<String, String> innerMap3 = new TreeMap<String, String>(){{
            put("Ai3-1", "Aiv3-1");
            put("Ai3-2", "Aiv3-2");
            put("Ai3-3", "Aiv3-3");
        }};
        doubleMapA.put("Ao1", innerMap1);
        doubleMapA.put("Ao2", innerMap2);
        doubleMapA.put("Ao3", innerMap3);
        l.add(doubleMapA);
        Map<String, Map<String, String>> doubleMapB = new TreeMap<>();
        Map<String, String> innerMapB1 = new TreeMap<String, String>(){{
            put("Bi1-1", "Biv1-1");
            put("Bi1-2", "Biv1-2");
            put("Bi1-3", "Biv1-3");
        }};
        Map<String, String> innerMapB2 = new TreeMap<String, String>(){{
            put("Bi2-1", "Biv2-1");
            put("Bi2-2", "Biv2-2");
            put("Bi2-3", "Biv2-3");
        }};
        Map<String, String> innerMapB3 = new TreeMap<String, String>(){{
            put("Bi3-1", "Biv3-1");
            put("Bi3-2", "Biv3-2");
            put("Bi3-3", "Biv3-3");
        }};
        doubleMapB.put("Bo1", innerMapB1);
        doubleMapB.put("Bo2", innerMapB2);
        doubleMapB.put("Bo3", innerMapB3);
        l.add(doubleMapB);
        //将两层结构的map变成一层的
        l.stream().flatMap(s -> s.values().stream()).forEach(e -> System.out.println(MapUtil.joinIgnoreNull(e, ",", "=")));
    }

    public boolean totalNumDONOTGreaterThan10(){
        return CartService.getCartSkuList().stream().noneMatch(s -> s.getTotalNum() > 10);
    }
}
